<!--
 * @页面名称:全宗权限
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div class="fondsManage">
    <!-- 按钮组 -->
    <!-- <div class="button-group">
      <el-button @click="save" type="primary">保存</el-button>
    </div> -->
    <div>
      <ztree :setting="treeSetting.setting" :nodes="treeSetting.treeData" @onCreated="onCreated"></ztree>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from "vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import Ztree from "@/components/ztree/Ztree.vue";
import { async } from "@antv/x6/lib/registry/marker/async";

const { proxy } = useCurrentInstance();
// 父级表格数据
const tabClickFromParent = ref();
// tree配置
const treeSetting = reactive({
  setting: {
    // ztree配置
    data: {
      simpleData: {
        enable: true,
        idKey: "ID",
        pIdKey: "FatherID"
      },
      key: {
        checked: "IsCheck",
        name: "Name"
      }
    },
    check: {
      enable: true
    },
    view: {
      showIcon: false
    }
  },
  treeData: [] as any[],
  ztreeObj: {} as any
});
// 获取全宗数据
const getFondsData = (record: any) => {
  tabClickFromParent.value = record;
  proxy.$vueAxios.get("MasterPermission/GetRolePermissionFondsFM", { gid: record.G_ID }, { noLoading: true }).then((res: any) => {
    treeSetting.treeData = res.List;
  });
};
// ztree初始化事件
const onCreated = (ztreeObj: any) => {
  treeSetting.ztreeObj = ztreeObj;
};
// 删除全宗数据
const delFonds = async () => {
  await proxy.$vueAxios.post(
    "Master_Base_Permission_FMT/DeleteByColumnName",
    {
      columnName: "G_ID",
      ids: [tabClickFromParent.value.G_ID]
    },
    { noLoading: true }
  );
  await proxy.$vueAxios.post(
    "Master_Base_Permission_FM/DeleteByColumnName",
    {
      columnName: "G_ID",
      ids: [tabClickFromParent.value.G_ID]
    },
    { noLoading: true }
  );
  await proxy.$vueAxios.post(
    "Master_Base_Permission_Fonds/DeleteByColumnName",
    {
      columnName: "G_ID",
      ids: [tabClickFromParent.value.G_ID]
    },
    { noLoading: true }
  );
};
// 新增全宗数据
const addFonds = async () => {
  const filterList = treeSetting.treeData.filter((v: any) => v.IsCheck); //获取所有勾选的数据
  const FondsParam = [] as any;
  const FMParam = [] as any;
  const FMTParam = [] as any;
  const FondsData = filterList.filter((v: any) => v.Type === "Fonds");
  const FMData = filterList.filter((v: any) => v.Type === "FM");
  const FMTData = filterList.filter((v: any) => v.Type === "FMT");

  FondsData.forEach((item: any) => {
    FondsParam.push({
      pF_ID: proxy.$global.createID(),
      g_ID: tabClickFromParent.value.G_ID,
      fonds_ID: item.Fonds_ID,
      pF_Status: item.Archive_Status
    });
  });
  FMData.forEach((item: any) => {
    FMParam.push({
      pfM_ID: proxy.$global.createID(),
      pF_ID: FondsParam.filter((v: any) => v.fonds_ID === FondsData.filter((v: any) => v.ID === item.FatherID)[0].Fonds_ID)[0]
        .pF_ID,
      g_ID: tabClickFromParent.value.G_ID,
      fonds_ID: item.Fonds_ID,
      fM_ID: item.FM_ID,
      pfM_Status: item.Archive_Status
    });
  });
  FMTData.forEach((item: any) => {
    // console.log(FMParam.filter((v: any) => v.fM_ID === FMData.filter((v: any) => v.FM_ID === item.FM_ID)[0].FM_ID)[0]);

    FMTParam.push({
      pF_ID: FMParam.filter((v: any) => v.fM_ID === FMData.filter((v: any) => v.FM_ID === item.FM_ID)[0].FM_ID)[0].pF_ID,
      pfM_ID: FMParam.filter((v: any) => v.fM_ID === FMData.filter((v: any) => v.FM_ID === item.FM_ID)[0].FM_ID)[0].pfM_ID,
      pfmT_ID: proxy.$global.createID(),
      g_ID: tabClickFromParent.value.G_ID,
      fonds_ID: item.Fonds_ID,
      fM_ID: item.FM_ID,
      fmT_ID: item.FMT_ID,
      pfmT_Status: item.Archive_Status
    });
  });
  await proxy.$vueAxios.post("Master_Base_Permission_Fonds/AddList", FondsParam, { noLoading: true });
  await proxy.$vueAxios.post("Master_Base_Permission_FM/AddList", FMParam, { noLoading: true });
  return await proxy.$vueAxios.post("Master_Base_Permission_FMT/AddList", FMTParam, { noLoading: true });
};
// 保存前先把改变数据反映到源数据
const observeChangeType = () => {
  if (treeSetting.ztreeObj) {
    const treeNodes = treeSetting.ztreeObj.getNodes();
    const simpleData = treeSetting.ztreeObj.transformToArray(treeNodes);
    simpleData.forEach((item: any) => {
      const index = treeSetting.treeData.findIndex((v: any) => v.ID === item.ID);
      if (index > -1) {
        treeSetting.treeData[index].IsCheck = item.IsCheck;
        treeSetting.treeData[index].PC_PermissionStr = item.PC_PermissionStr;
      }
    });
  }
};
// 保存
const save = async () => {
  proxy.$showLoading({ tip: "正在保存" });
  observeChangeType();
  await delFonds();
  const res = await addFonds();
  if (res.Mark) {
    proxy.$message.success("保存成功");
  } else {
    proxy.$message.error("保存失败");
  }
  proxy.$hideLoading();
};
// 将函数暴露给父组件
defineExpose({ getFondsData, save });
</script>

<style scoped lang="scss">
.fondsManage {
  height: calc(100vh - 140px);
  padding-right: 6px;
  margin-left: 10px;
  overflow-y: scroll;
}
</style>
